package learn.spring;

import static java.util.concurrent.TimeUnit.NANOSECONDS;

import java.util.SplittableRandom;

/**
 * 工具：执行时间
 *  
 * @author PinWei Wan
 * @since 2.6.9
 */
public class Timer {
    private final static SplittableRandom RANDOM = new SplittableRandom(47);

    private long start = System.nanoTime();

    public long duration() {
      return NANOSECONDS.toMillis(
          System.nanoTime() - start);
    }
  
    public static long duration(Runnable test) {
      Timer timer = new Timer();
      test.run();
      return timer.duration();
    }  
    
    /**
     * 模拟业务执行
     */
    public static long mock() {
        return Timer.duration(() -> new Nap(RANDOM.nextInt(1000)));
    }
}
